package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrderService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class OrderServiceImpl implements OrderService {
    @Override
    public int createOrders(Orders orders) {
        int orderId = 0;
        int result = 0;
        OrdersDao ordersDao = new OrdersDaoImpl();
        CartDao cartDao = new CartDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        try {
            DBUtil.getConnection();
            DBUtil.beginTransaction();
            orders.setOrderState(0);
            orderId = ordersDao.createOrders(orders);
            List<Cart> carts = cartDao.listCart(orders.getUserId(), orders.getBusinessId());
            List<Orderdetailet> orderdetailets = new ArrayList<>();
            for (Cart cart : carts) {
                Orderdetailet orderdetailet = new Orderdetailet();
                orderdetailet.setFoodId(cart.getFoodId());
                orderdetailet.setOrderId(orderId);
                orderdetailet.setQuantity(cart.getQuantity());
                orderdetailets.add(orderdetailet);
            }
            orderdetailetDao.saveOrderdetailet(orderdetailets);
            result = cartDao.removeCart(orders.getUserId(), orders.getBusinessId());

            DBUtil.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.close();
        }

        return orderId;
    }

    @Override
    public Orders getOrdersById(int orderId) {
        Orders orders = new Orders();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        try {
            DBUtil.getConnection();
            orders = ordersDao.getOrdersById(orderId);
//            List<Orderdetailet> orderdetailet = orderdetailetDao.getOrderdetailet(orderId);
//            orders.setList(orderdetailet);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders> list = new ArrayList<>();
        OrdersDao dao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        try {
            DBUtil.getConnection();
            list = dao.listOrdersByUserId(userId);
//            for (Orders order : list) {
//                List<Orderdetailet> orderdetailet = orderdetailetDao.getOrderdetailet(order.getOrderId());
//                order.setList(orderdetailet);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
}
